package com.sxkj.gis.wms.getmap.controller;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.sxkj.gis.wms.bean.UrlBean;
import com.sxkj.gis.wms.bean.WmsParams;
import com.sxkj.gis.wms.comm.service.WmsCommService;
import com.sxkj.gis.wms.getmap.service.WmsGetMapService;
import com.sxkj.gis.wms.service.UrlBeanService;

@Controller
public class GetMapController extends Thread {
	@Resource
	private UrlBeanService urlBeanService;
	
	@Resource 
	private WmsGetMapService wmsService;
	@Resource
	private WmsCommService wmsCommService;
	/**
	 * 图片保存的根路径
	 */
	private static String  basePath = GetMapController.class.getClassLoader().getResource("data/").getPath();
	
	/**
	 * 图片保存的根路径下的目录
	 */
	private static String saveDir = "img";
	
	private static Integer i = 1;
	
	private Map<Integer,String> picNameMap = new HashMap<Integer,String>();
	
	/**
	 * 选择request获取不同的链接和body
	 * @param urlBean
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getUrl.do")
	public UrlBean getUrl(UrlBean urlBean){
		UrlBean urlBean2 = urlBeanService.getByRequest(urlBean);
		System.out.println(urlBean2.getUrl());
		return urlBean2;
	}
	
	
	@ResponseBody
	@RequestMapping(value="/WMS-getMap-url.do")
	public Integer getMap1(UrlBean url,WmsParams getMapParams,HttpSession session,HttpServletRequest rq,HttpServletResponse rp){
		String picName = System.currentTimeMillis()+".png";
		picNameMap.put(i, picName);
		System.out.println(basePath);
		try{
			ByteArrayOutputStream byteArrayOutputStream =wmsService.WMSgetMapurl(url,getMapParams,session);
			byte [] buffer = byteArrayOutputStream.toByteArray();
			//保存之前，先删除上次保存的
			wmsCommService.deleteFile(picNameMap.get(i-1),basePath,saveDir);
			//保存图片
			wmsCommService.saveFile(picName,buffer, basePath,saveDir);
			return 1;
		}catch(Exception e){
			e.printStackTrace();
		}
		return 2;
	}
	
	@ResponseBody
	@RequestMapping(value="/WMS-getMap-xml.do")
	public Integer getMap2(UrlBean url,WmsParams getMapParams,HttpSession session,HttpServletRequest rq,HttpServletResponse rp){
		String picName = System.currentTimeMillis()+".png";
		picNameMap.put(i, picName);
		System.out.println(basePath);
		try{
			ByteArrayOutputStream byteArrayOutputStream =wmsService.WMSgetMapxml(url,getMapParams,session);
			byte [] buffer = byteArrayOutputStream.toByteArray();
			//保存之前，先删除上次保存的
			wmsCommService.deleteFile(picNameMap.get(i-1),basePath,saveDir);
			//保存图片
			wmsCommService.saveFile(picName,buffer, basePath,saveDir);
			return 1;
		}catch(Exception e){
			e.printStackTrace();
		}
		return 2;
	}
	
	@ResponseBody
	@RequestMapping(value="/WMS-getMap-OpenLayers-url.do")
	public Integer getMap3(UrlBean url,WmsParams getMapParams,HttpSession session,HttpServletRequest rq,HttpServletResponse rp){
		String picName = System.currentTimeMillis()+".png";
		picNameMap.put(i, picName);
		System.out.println(basePath);
		try{
			ByteArrayOutputStream byteArrayOutputStream =wmsService.WMSgetMapurl(url,getMapParams,session);
			byte [] buffer = byteArrayOutputStream.toByteArray();
			//保存之前，先删除上次保存的
			wmsCommService.deleteFile(picNameMap.get(i-1),basePath,saveDir);
			//保存图片
			wmsCommService.saveFile(picName,buffer, basePath,saveDir);
			return 1;
		}catch(Exception e){
			e.printStackTrace();
		}
		return 0;
	}
	
	@ResponseBody
	@RequestMapping(value="/WMS-getMap-SLD-BODY-LiteralMode-1.3.url.do")
	public Integer getMap4(UrlBean url,WmsParams getMapParams,HttpSession session,HttpServletRequest rq,HttpServletResponse rp){
		String picName = System.currentTimeMillis()+".png";
		picNameMap.put(i, picName);
		System.out.println(basePath);
		try{
			ByteArrayOutputStream byteArrayOutputStream =wmsService.WMSgetMapurl(url,getMapParams,session);
			byte [] buffer = byteArrayOutputStream.toByteArray();
			//保存之前，先删除上次保存的
			wmsCommService.deleteFile(picNameMap.get(i-1),basePath,saveDir);
			//保存图片
			wmsCommService.saveFile(picName,buffer, basePath,saveDir);
			return 1;
		}catch(Exception e){
			e.printStackTrace();
		}
		return 0;
	}
	
	@ResponseBody
	@RequestMapping(value="/WMS-getMap-SLD-BODY-LiteralMode.url.do")
	public Integer getMap5(UrlBean url,WmsParams getMapParams,HttpSession session,HttpServletRequest rq,HttpServletResponse rp){
		String picName = System.currentTimeMillis()+".png";
		picNameMap.put(i, picName);
		System.out.println(basePath);
		try{
			ByteArrayOutputStream byteArrayOutputStream =wmsService.WMSgetMapurl(url,getMapParams,session);
			byte [] buffer = byteArrayOutputStream.toByteArray();
			//保存之前，先删除上次保存的
			wmsCommService.deleteFile(picNameMap.get(i-1),basePath,saveDir);
			//保存图片
			wmsCommService.saveFile(picName,buffer, basePath,saveDir);
			return 1;
		}catch(Exception e){
			e.printStackTrace();
		}
		return 0;
	}
	
	@ResponseBody
	@RequestMapping(value="/WMS-getMap-SVG.url.do")
	public Integer getMap6(UrlBean url,WmsParams getMapParams,HttpSession session,HttpServletRequest rq,HttpServletResponse rp){
		String picName = System.currentTimeMillis()+".svg";
		picNameMap.put(i, picName);
		try{
			ByteArrayOutputStream byteArrayOutputStream =wmsService.WMSgetMapSVG(url,getMapParams,session);
			byte [] buffer = byteArrayOutputStream.toByteArray();
			//保存之前，先删除上次保存的
			wmsCommService.deleteFile(picNameMap.get(i-1),basePath,saveDir);
			//保存图片
			wmsCommService.saveFile(picName,buffer, basePath,saveDir);
			return 2;
		}catch(Exception e){
			e.printStackTrace();
		}
		return 0;
	}
	
	
	@ResponseBody
	@RequestMapping(value="/getSVGimg.do")
	public String getSVG(HttpServletResponse rp,HttpServletRequest rq){
		try{
			String picName = picNameMap.get(i);
			i++;
			byte [] buff = wmsCommService.getFile(picName,basePath,saveDir);
			rp.getOutputStream().write(buff);
			rp.getOutputStream().flush();
			rp.getOutputStream().close();
		}catch(Exception e){
			e.printStackTrace();
		}
		return "success";
	}
	
	/**
	 * 将图片返回到页面
	 * @param rp
	 */
	@ResponseBody
	@RequestMapping(value="/getPic.do")
	public void getPic(HttpServletResponse rp,HttpServletRequest rq){
		try{
			String picName = picNameMap.get(i);
			i++;
			byte [] buff = wmsCommService.getFile(picName,basePath,saveDir);
			rp.getOutputStream().write(buff);
			rp.getOutputStream().flush();
			rp.getOutputStream().close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 测试svg图片下载
	 * @param rp
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value="/getSvg.do")
	public void testSVG(HttpServletResponse rp) throws IOException{
		File file = new File(basePath+"img/jiuba.svg");
		FileInputStream inputStream = new FileInputStream(file);
		int i= inputStream.available();
		byte[] buff = new byte[i];
		
		inputStream.read(buff);
		rp.getOutputStream().write(buff);
		rp.getOutputStream().flush();
		rp.getOutputStream().close();
	}	
	
	
	/**
	 * 图片上传测试
	 * @param file
	 * @return
	 * @throws IOException
	 */
	 @RequestMapping("/fileUpload.do")
	  public String fileUpload(@RequestParam("file") CommonsMultipartFile file) throws IOException {
		 System.out.println("---------------->");
	    //用来检测程序运行时间
	    long startTime=System.currentTimeMillis();
	    System.out.println("fileName："+file.getOriginalFilename());
	    try {
	      //获取输出流
	      OutputStream os=new FileOutputStream("E:\\新建文件夹\\"+new Date().getTime()+file.getOriginalFilename());
	      //获取输入流 CommonsMultipartFile 中可以直接得到文件的流
	      InputStream is=file.getInputStream();
	      int temp;
	      //一个一个字节的读取并写入
	      while((temp=is.read())!=(-1))
	      {
	        os.write(temp);
	      }
	      os.flush();
	      os.close();
	      is.close();
	    } catch (FileNotFoundException e) {
	      // TODO Auto-generated catch block
	      e.printStackTrace();
	    }
	    long endTime=System.currentTimeMillis();
	    System.out.println("方法一的运行时间："+String.valueOf(endTime-startTime)+"ms");
	    return "/success"; 
	  } 
}
